﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using RM2012Server.Business;

namespace RM2012Server.Model
{
    public class MaterialDAO
    {
        DataServices m_MaterialDAO = new DataServices();

        public DataTable GetAll()
        {
            m_MaterialDAO.Load("GetAllMaterial", null);
            return m_MaterialDAO;
        }

        public DataTable GetById(String materialId)
        {
            SqlParameter param = new SqlParameter("@MaterialId", SqlDbType.VarChar, 6);
            param.Value = materialId;

            m_MaterialDAO.Load("GetMaterialById", param);
            return m_MaterialDAO;
        }

        public DataTable GetByLikeId(String materialId)
        {
            SqlParameter param = new SqlParameter("@MaterialId", SqlDbType.VarChar, 6);
            param.Value = materialId;

            m_MaterialDAO.Load("GetMaterialByLikeId", param);
            return m_MaterialDAO;
        }

        public DataTable GetByName(String materialName)
        {
            SqlParameter param = new SqlParameter("@MaterialName", SqlDbType.NVarChar, 100);
            param.Value = materialName;

            m_MaterialDAO.Load("GetMaterialByName", param);
            return m_MaterialDAO;
        }

        public DataTable GetByOption(String materialId, String materialName, String cateId, String unitId, bool price, double minPrice, double maxPrice)
        {
            SqlParameter[] param = new SqlParameter[7];

            param[0] = new SqlParameter("@MaterialId", SqlDbType.VarChar, 6);
            param[0].Value = materialId;

            param[1] = new SqlParameter("@MaterialName", SqlDbType.NVarChar, 100);
            param[1].Value = materialName;

            param[2] = new SqlParameter("@CateId", SqlDbType.VarChar, 6);
            param[2].Value = cateId;

            param[3] = new SqlParameter("@UnitId", SqlDbType.VarChar, 6);
            param[3].Value = unitId;

            param[4] = new SqlParameter("@Price", SqlDbType.Bit);
            param[4].Value = price;

            param[5] = new SqlParameter("@PriceMin", SqlDbType.Money);
            param[5].Value = minPrice;

            param[6] = new SqlParameter("@PriceMax", SqlDbType.Money);
            param[6].Value = maxPrice;

            m_MaterialDAO.Load("GetMaterialFull", param);
            return m_MaterialDAO;
        }

        public int Add(MaterialInfo info)
        {
            SqlParameter[] param = new SqlParameter[10];
            param[0] = new SqlParameter("@MaterialId", SqlDbType.VarChar, 6);
            param[0].Value = info.MaterialId;

            param[1] = new SqlParameter("@MaterialName", SqlDbType.NVarChar, 100);
            param[1].Value = info.MaterialName;

            param[2] = new SqlParameter("@CateId", SqlDbType.VarChar, 6);
            param[2].Value = info.CateId;

            param[3] = new SqlParameter("@UnitId", SqlDbType.VarChar, 6);
            param[3].Value = info.UnitId;

            param[4] = new SqlParameter("@Price", SqlDbType.Real);
            param[4].Value = info.Price;

            param[5] = new SqlParameter("@State", SqlDbType.Bit);
            param[5].Value = info.State;

            param[6] = new SqlParameter("@IsMenu", SqlDbType.Bit);
            param[6].Value = info.IsMenu;

            param[7] = new SqlParameter("@WarningQuantity", SqlDbType.Real);
            param[7].Value = info.WarningQuantity;

            param[8] = new SqlParameter("@StockQuantity", SqlDbType.Real);
            param[8].Value = info.StockQuantity;

            param[9] = new SqlParameter("@Remark", SqlDbType.NVarChar, 500);
            param[9].Value = info.Remark;

            return m_MaterialDAO.ExecuteUpdate("AddMaterial", param);
        }

        public int Update(MaterialInfo info)
        {
            SqlParameter[] param = new SqlParameter[10];
            param[0] = new SqlParameter("@MaterialId", SqlDbType.VarChar, 6);
            param[0].Value = info.MaterialId;

            param[1] = new SqlParameter("@MaterialName", SqlDbType.NVarChar, 100);
            param[1].Value = info.MaterialName;

            param[2] = new SqlParameter("@CateId", SqlDbType.VarChar, 6);
            param[2].Value = info.CateId;

            param[3] = new SqlParameter("@UnitId", SqlDbType.VarChar, 6);
            param[3].Value = info.UnitId;

            param[4] = new SqlParameter("@Price", SqlDbType.Real);
            param[4].Value = info.Price;

            param[5] = new SqlParameter("@State", SqlDbType.Bit);
            param[5].Value = info.State;

            param[6] = new SqlParameter("@IsMenu", SqlDbType.Bit);
            param[6].Value = info.IsMenu;

            param[7] = new SqlParameter("@WarningQuantity", SqlDbType.Real);
            param[7].Value = info.WarningQuantity;

            param[8] = new SqlParameter("@StockQuantity", SqlDbType.Real);
            param[8].Value = info.StockQuantity;

            param[9] = new SqlParameter("@Remark", SqlDbType.NVarChar, 500);
            param[9].Value = info.Remark;

            return m_MaterialDAO.ExecuteUpdate("UpdateMaterial", param);
        }

        public int Delete(String materialId)
        {
            SqlParameter param = new SqlParameter("@MaterialId", SqlDbType.VarChar, 6);
            param.Value = materialId;

            return m_MaterialDAO.ExecuteUpdate("DeleteMaterial", param);
        }

        public DataRow AddNewRow()
        {
            return m_MaterialDAO.NewRow();
        }

        public void AddNewMaterial(DataRow m_Row)
        {
            m_MaterialDAO.Rows.Add(m_Row);
        }

        public bool SaveData()
        {
            return m_MaterialDAO.ExecuteNoneQuery() > 0;
        }
    }
}
